from datetime import datetime
from pytz import timezone
from model.wristband_models import LocationData
from utils.date_utils import timestamp_to_str_000z
from utils.mongo_util import db


async def save_location_mongo(in_fence: bool, location_data: LocationData, distance: float):
    beijing_tz = timezone('Asia/Shanghai')
    date = datetime.fromtimestamp(location_data.timestamp, beijing_tz)
    location_redis = {
        "imei": location_data.device_id,
        "lat": str(location_data.latitude),
        "lon": str(location_data.longitude),
        "fix": "BT",
        "fixedPosition": 1,
        "ctm": timestamp_to_str_000z(location_data.timestamp),
        "date": date,
        "in_fence": in_fence,
        "distance": distance
    }
    db.p_wristband_location.insert_one(location_redis)